package com.ophiux.cs.application.user.impl.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.application.user.dto.addUserInfoRequest;
import com.ophiux.cs.application.user.dto.addUserInfoResponse;
import com.ophiux.cs.domain.user.domainservice.UserDomainService;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.model.valueobj.UserValueObj;
import com.ophiux.cs.domain.user.repositories.IUserRepository;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class addUserPatientInfoService {
	@Autowired
	IUserRepository iUserRepository;
	@Autowired
	UserDomainService userDomainService;

	public Result<addUserInfoResponse> addUserInfo(addUserInfoRequest dto) {
		Result<addUserInfoResponse> result = new Result<addUserInfoResponse>();
		addUserInfoResponse res = new addUserInfoResponse();
		try {
			try {
				// 验证参数是否允许为空
				dto.paramVer();
			} catch (ParamException e) {
				return Result.fail(ResultEnum.requestParamFail.getCode(), e.getMessage());
			}

			/**
			 * 根据电话号码和角色     判断是否已经注册
			 */
			UserValueObj obj = new UserValueObj();
			obj.setUSER_ACC(dto.getPHONE());
			obj.setRolePatient();

			User userList = iUserRepository.getUser(obj);
			if (userList == null) {
				User userInfo = new User();
				userInfo.setUSER_ACC(dto.getPHONE());
				userInfo.setPHONE(dto.getPHONE());
				userInfo.setUSER_PWD(dto.getUSER_PWD());
				userInfo.setRolePATIENT();

				boolean saveResult = iUserRepository.savaUser(userInfo);
				if (saveResult) {// 保存成功
					return result = Result.success(res);
				} else {
					result = Result.fail(ResultEnum.registerFail.getCode(), ResultEnum.registerFail.getMsg());
				}
			} else {
				return Result.fail(ResultEnum.userAgin.getCode(), ResultEnum.userAgin.getMsg());
			}

		} catch (Exception e) {
			log.info("addUserInfo接口异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}

		return result;
	}

}
